using System;
using System.IO;
using System.Text;
using System.Collections;
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;

namespace Microsoft.CommerceServer.SecureSite
{
	public class SecureCommerceDB : Microsoft.CommerceServer.SecureSite.Wizard
	{
		private System.Windows.Forms.Panel pnlCSDBs;
		private System.Windows.Forms.Label lblDBInstructions;
		private System.Windows.Forms.Button btnSaveConfig;
		private System.Windows.Forms.SaveFileDialog fdlgSecureCS;
		private System.ComponentModel.IContainer components = null;

		public SecureCommerceDB()
		{
			// This call is required by the Windows Form Designer.
			InitializeComponent();
		}

		/// <summary>
		/// Clean up any resources being used.
		/// </summary>
		protected override void Dispose( bool disposing )
		{
			if( disposing )
			{
				if (components != null) 
				{
					components.Dispose();
				}
			}
			base.Dispose( disposing );
		}

		#region Designer generated code
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{
            this.Load += new System.EventHandler(this.SecureCommerceDB_Load);
		}
		#endregion

		private void SecureCommerceDB_Load(object sender, System.EventArgs e)
		{
            this.fdlgSecureCS = new System.Windows.Forms.SaveFileDialog();
            this.pnlCSDBs = new System.Windows.Forms.Panel();
            this.lblDBInstructions = new System.Windows.Forms.Label();
            this.btnSaveConfig = new System.Windows.Forms.Button();
            ((System.ComponentModel.ISupportInitialize)(this.MasterErrorProvider)).BeginInit();
            this.pnlCSDBs.SuspendLayout();
            this.SuspendLayout();
            // 
            // pnlCSDBs
            // 
            this.pnlCSDBs.Controls.Add(this.lblDBInstructions);
            this.pnlCSDBs.Controls.Add(this.btnSaveConfig);
            this.pnlCSDBs.Dock = System.Windows.Forms.DockStyle.Fill;
            this.pnlCSDBs.Location = new System.Drawing.Point(0, 91);
            this.pnlCSDBs.Name = "pnlCSDBs";
            this.pnlCSDBs.Size = new System.Drawing.Size(552, 243);
            this.pnlCSDBs.TabIndex = 4;
            // 
            // lblDBInstructions
            // 
            this.lblDBInstructions.Location = new System.Drawing.Point(40, 16);
            this.lblDBInstructions.Name = "lblDBInstructions";
            this.lblDBInstructions.Size = new System.Drawing.Size(472, 195);
            this.lblDBInstructions.TabIndex = 0;
            this.lblDBInstructions.Text = "label1";
            // 
            // btnSaveConfig
            // 
            this.btnSaveConfig.Location = new System.Drawing.Point(448, 214);
            this.btnSaveConfig.Name = "btnSaveConfig";
            this.btnSaveConfig.Size = new System.Drawing.Size(88, 23);
            this.btnSaveConfig.TabIndex = 5;
            this.btnSaveConfig.Text = "Save Config";
            this.btnSaveConfig.Click += new System.EventHandler(this.btnSaveConfig_Click);
            // 
            // SecureCommerceDB
            // 
            this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
            this.ClientSize = new System.Drawing.Size(552, 374);
            this.Controls.Add(this.pnlCSDBs);
            this.Name = "SecureCommerceDB";
            this.Controls.SetChildIndex(this.pnlCSDBs, 0);
            ((System.ComponentModel.ISupportInitialize)(this.MasterErrorProvider)).EndInit();
            this.pnlCSDBs.ResumeLayout(false);
            this.ResumeLayout(false);

			CommerceSecurityInfo.Log("FORM: Secure Commerce Databases form loaded");
			base.Title = "Securing Commerce Server Databases";
			base.btnNext.Text = "Finish";
            this.lblDBInstructions.Text += "The following SQL Server 2005 Databases will be secured with Commerce Server built-in roles. \r\n\r\n";
            this.lblDBInstructions.Text += "   - MSCS_Admin\r\n";
            this.lblDBInstructions.Text += "   - msdb\r\n";
            this.lblDBInstructions.Text += "   - MSCS_CatalogScratch\r\n";
            this.lblDBInstructions.Text += "   - <site_name>_ProductCatalog\r\n";
            this.lblDBInstructions.Text += "   - <site_name>_Marketing\r\n";
            this.lblDBInstructions.Text += "   - <site_name>_MarketingLists\r\n";
            this.lblDBInstructions.Text += "   - <site_name>_Profiles\r\n";
            this.lblDBInstructions.Text += "   - <site_name>_TransactionConfig\r\n";
            this.lblDBInstructions.Text += "   - <site_name>_Transactions\r\n";
            this.lblDBInstructions.Text += "   - <sitename>_DataWarehouse\r\n";
            this.lblDBInstructions.Text += "\r\nClick the Finish button to apply the security settings";
		}

		protected override void btnPrevious_Click(object sender, System.EventArgs e)
		{
			CommerceSecurityInfo.MoveToForm("ProfilesWebService");
			base.Visible = false;
		}

		protected override void btnNext_Click(object sender, System.EventArgs e)
		{
			if (base.IsValid)
			{
				base.Visible = false;
				CommerceSecurityInfo.MoveToForm("Completed");
			}
		}

		private void btnSaveConfig_Click(object sender, System.EventArgs e)
		{
			fdlgSecureCS.Filter = "Xml files (*.xml)|*.xml|All files (*.*)|*.*";
			fdlgSecureCS.InitialDirectory = Application.ExecutablePath;

			if(DialogResult.OK == fdlgSecureCS.ShowDialog(this))
			{
				StreamWriter st = File.CreateText(fdlgSecureCS.FileName);
				CommerceSecurityInfo.Log("INFO: "+fdlgSecureCS.FileName+" file created.");
				string xmlData = CommerceSecurityInfo.GenerateSecurityFile();
				st.Write(xmlData);
				st.Close();
				CommerceSecurityInfo.Log("INFO: "+fdlgSecureCS.FileName+" security data saved.");
			}
		}
	}
}

